const req = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nodes:{},
    id:"",
    title:"",
    img:'../../img/fenxiang.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let sessionId = wx.getStorageSync("token")
    if (sessionId !== "" && sessionId!==null){
      this.getDetais(options.cid)
      wx.setNavigationBarTitle({
        title: options.title,
        id: options.cid
      })
    }else{
      this.getToken()
      setTimeout(()=>{
        this.getDetais(options.cid)
        wx.setNavigationBarTitle({
          title: options.title,
          id: options.cid
        })
      },1000)
    }
 
  },
  getToken() {
    let self = this
    wx.login({
      success: res => {
        wx.request({
          url: 'http://106.12.213.229:9501/api/v1/user/getLoginToken',
          data: {
            code: res.code
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
          },
          method: 'POST',
          success: function (res) {

            wx.setStorageSync('token', res.data.result.token);

          }
        })
      }
    })
  },
getDetais(id){
  let that=this
  wx.showLoading({
    title: '加载中',
  })
  req.networkget("v1/article/getArticleDetails",{id:id}).then(res => {
    console.log(res,"78787")
    if (res.data.result_code == 200) {
      that.setData({
        id: res.data.result.aid,
        nodes: res.data.result,
        title: res.data.result.title
      })
      wx.hideLoading()
    } else {
      wx.showToast({
        title: res.data.message,
      })
      wx.hideLoading()
    }
    console.log(res, "列表")
  }).catch(res => {
    wx.showToast({
      title: "请求失败"
    })
  })
},
  onShareAppMessage: function (res) {
    var that = this;
    var title = that.data.title; //获取产品标题
    var img = title = that.data.img;
    var id = title = that.data.id; 
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: title,
        path: '../../pages/details/details?cid=' + id,
        imageUrl: img, //不设置则默认为当前页面的截图
        success: (res) => {
          wx.showToast({ title: res, icon: 'success', duration: 2000 })
        },
        fail: (res) => {
          wx.showToast({ title: res, icon: 'success', duration: 2000 })
        }
      }
    }
  },

love(){
  req.networkpost("v1/collect/addCollect", { id:this.data.id }).then((res) => {
    if (res.data.result_code == 200) {
      console.log(res)
      wx.showToast({
        title: '收藏成功',
      })
    } else {
      wx.showToast({
        title: res.data.message,
      })
    }
  });
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  details_jump(e) {
    let title = e.currentTarget.dataset.title;
    let cid = e.currentTarget.dataset.aid;
    wx.navigateTo({
      url: '../../pages/details/details?title=' + title + "&cid=" + cid,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})